import { Directive, ElementRef, Input } from '@angular/core';

@Directive({
    selector: '[absIf]'
})
export class AbsIfDirective {
    @Input() set absIf(absIf: boolean) {
        this.refreshState(absIf);
    }

    constructor(private _element: ElementRef) {}

    refreshState(absIf: boolean): void {
        if (absIf === undefined) {
            return;
        }

        if (absIf) {
            debugger;
            this._element.nativeElement.setAttribute('min', 0);

            this._element.nativeElement.addEventListener('keyup', event => {
                let num = event.target.value;
                if (num < 0) { event.target.value = -num; }
                //  event.target.value = Math.abs(Number(num));
            });
        }
    }
}
